<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	// using here requires because of the early load time in the bootstrap process
	require_once TEKUNA_ROOT .'org/tekuna/base/exceptions/LogException.class.php';
	require_once TEKUNA_ROOT .'org/tekuna/base/log/TekunaLogger.interface.php';


	/**
	 * Simple logger implementation that is used by the Tekuna base.
	 */

	class SimpleLogger implements TekunaLogger {

		protected $sFileName;


		/**
		 * Create a new logger class for the given file name
		 *
		 * @param string $sFileName the logging file name
		 */

		public function __construct($sFileName) {

			$this -> setFileName($sFileName);
		}


		/**
		 * @return the current file name
		 */

		public function getFileName() {

			return $this -> sFileName;
		}


		/**
		 * set a new file name
		 *
		 * @param string $sFileName the new file name
		 */

		public function setFileName($sFileName) {

			$this -> sFileName = $sFileName;
		}


		/**
		 * Log the given message into the file on a new line.
		 * The current date and time are prepended automatically.
		 *
		 * @param string $sMessage the log message
		 */

		public function log($sMessage) {

			// build log line
			$sLine = date('Y-m-d H:i:s');
			$sLine .= "\t";
			$sLine .= $sMessage;
			$sLine .= "\n";


			// open log file
			$hFile = fopen($this -> getFileName(), 'a');

			if ($hFile === FALSE) {

				throw new LogException("Could not open log file '". $this -> getFileName() ."' for writing.");
			}

			// write the log line
			if (! fwrite($hFile, $sLine)) {

				throw new LogException("Could not write into log file '". $this -> getFileName() ."'.");
			}

			// close log file
			if (! fclose($hFile)) {

				throw new LogException("Could not close log file '". $this -> getFileName() ."' after writing.");
			}
		}


		/**
		 * return the content of the current log file in a
		 * pre-parsed array format. Each line is represented by
		 * a new entry in the array. Each entry is an array again
		 * that holds the following two keys: date, message. The
		 * date is not parsed into a unix timestamp and comes in
		 * the stored raw format.
		 *
		 * @return array the data array
		 */

		public function getLogData() {

			$arrLines = file($this -> getFileName());
			$arrData = array();

			foreach ($arrLines as $sLine) {

				list($sDate, $sMessage) = explode("\t", $sLine, 2);

				$sDate = trim($sDate);
				$sMessage = trim($sMessage);

				$arrData[] = array('date' => $sDate, 'message' => $sMessage);
			}

			return $arrData;
		}
	}
